<?php

namespace admin\dao;

/**--tmpl-use--**/
use herosphp\core\Loader;

/**--tmpl-import--**/

class RbacDao extends CommonDao{

	/**--tmpl-vars--**/

	private $adminManagerDao = null;
	private $adminRoleDao = null;
	private $adminNodeDao = null;
	private $adminManagerRoleDao = null;
	private $adminRoleNodeDao = null;

	
	/**--tmpl-construct--**/

	/**-- daoConstruct --**/
	public function __construct($adminManagerModel, $adminRoleModel, $adminNodeModel, $adminManagerRoleModel, $adminRoleNodeModel) {
				
			$this->setModelDao(Loader::model($adminManagerModel));
			$this->adminRoleDao = Loader::model($adminRoleModel);
			$this->adminNodeDao = Loader::model($adminNodeModel);
			$this->adminManagerRoleDao = Loader::model($adminManagerRoleModel);
			$this->adminRoleNodeDao = Loader::model($adminRoleNodeModel);
	}
	

	//获取用户拥有的权根数组
	public function getUserNodes($userid){

		static $lifeCache = array();
		if($lifeCache[$userid]){
			return $lifeCache[$userid];
		}
		$userinfo = $this->getItem($userid);

		$role_id = $userinfo['role_id'];
		if($role_id == 0){
			//超级管理员
			return $this->adminNodeDao->getNodeArray();

		}

		$rs = $this->adminRoleNodeDao->getItems(array('role_id' => $role_id),array('node_id'));
		$node_ids = array();
		foreach($rs as $v){
			$node_ids[] = $v['node_id'];
		}
		$nodes = $this->adminNodeDao->getItems(array('id' => array('$in' => $node_ids)));
		$lifeCache[$userid] = $nodes;
		return $nodes;
	}

	/*
	 * @override
	 */
	public function getItem($conditions, $fields, $order)
	{
		if(is_array($conditions)){
			$conditions['del_status'] = 0;
		}
		else{
			$id = $conditions;
			$conditions = array('del_status' => 0,'id' => $id);
		}
		$item = parent::getItem($conditions, $fields, $order);
		if(!$item) return NULL;
		/**--tmpl-todo--**/
	
		/**-- foreignitem --**/
		$admin_manager_role = $this->adminManagerRoleDao->getItem($item['role_id']);
		
		$item['role_name'] = $admin_manager_role['name'];
		//----------------------
		/**--tmpl-daoassoctodo--**/
		return $item;
	}
	
	/*
	 * @override 
	 */
	public function getItems($conditions, $fields, $order, $limit, $group, $having)
	{
		$conditions['del_status'] = 0;
		$items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
		if(!$items) return NULL;
	
		/**--tmpl-todo--**/
	
		/**-- foreigncondi --**/
		$admin_manager_rolecondi = array();
		$admin_manager_managercondi = array();
		foreach($items as $k => $v)
		{
			if($v['role_id'])
			{
				$admin_manager_rolecondi[] = $v['role_id'];	
			}	
			if($v['manager_id'])
			{
				$admin_manager_managercondi[] = $v['manager_id'];
			}
		}
		
		$admin_roles = $this->adminRoleDao->getItems(array('id' => array( '$in' => $admin_manager_rolecondi)));
		$admin_manager = $this->modelDao->getItems(array('id' => array('$in' => $admin_manager_managercondi)));
		
		/**-- foreignforeach  --**/
		foreach ($items as $k => $v)
		{
			/**--tmpl-foreignfill--**/
		
			/**-- foreigneach --**/
			foreach($admin_roles as $kk => $vv)
			{
				if($vv['id'] == $v['role_id'])
				{
					/**--tmpl-fieldfill--**/
			
					$items[$k]['role_name'] = $vv['name'];
			
				}
			}
			foreach($admin_manager as $kk => $vv)
			{
				if($vv['id'] == $v['manager_id'])
				{
					$items[$k]['manager_name'] = $vv['name'];
				}
			}
			
		}
		return $items;
	}

	public function count($conditions)
	{
		
		$conditions['del_status'] = 0;
		return parent::count($conditions);
	}
	
	// see NodeMode::getNodeArray
	public function getNodeArray(){
		return $this->adminNodeDao->getNodeArray();	
	}

	/*
	 * 软删除
	 */
	public function delete($id)
	{
		return  parent::update(array('del_status' => 1),$id);
	}

	/*
	 * 软删除
	 */
	public function deletes($conditions)
	{
		return parent::updates(array('del_status' => 1),$conditions);	
	}

}
